﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.BusinessLayer;

public partial class MyDrafts : PageBaseClass
{
    UploadedDocumentFactory uDocFactory;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DepartmentFactory dFactory = new DepartmentFactory();
            List<Department> deptList = dFactory.GetAllBy(Department.DepartmentFields.Office_Code, _author.office_Code);

            ddlDept.DataSource = deptList;
            ddlDept.DataTextField = Department.DepartmentFields.Department_Name.ToString();
            ddlDept.DataValueField = Department.DepartmentFields.Department_Code.ToString();
            ddlDept.DataBind();

            if (deptList.Count > 0)
            {
                ddlDept.SelectedIndex = 0;
                BindTheUsers(int.Parse(ddlDept.SelectedValue));
            }
            else
            {
                BindTheUsers(0);
            }
        }

        MailMessageFactory mmFactory = new MailMessageFactory();
        List<MailMessage> msgs = mmFactory.GetAllBy(MailMessage.MailMessageFields.Owner, _author.user_Code);

        gvMessages.DataSource = msgs;
        gvMessages.DataBind();

        BindTheUD(_author.user_Code);
        //DocumentTypeFactory docTypeFactory = new DocumentTypeFactory();
        //List<DocumentType> docTypes = docTypeFactory.GetAllBy(DocumentType.DocumentTypeFields.Office_Code, _author.office_Code);

        //uDocFactory = new UploadedDocumentFactory();
        //List<UploadedDocument> uDocs = uDocFactory.GetAllBy(UploadedDocument.UploadedDocumentFields.Owner, _author.user_Code);

        //gvUploadedDocs.DataSource = uDocs;
        //gvUploadedDocs.DataBind();
    }

    private void BindTheUsers(int deptCode)
    {
        if (deptCode >= 0)
        {
            this.ddlUsers.DataSource = new UserInfoFactory().GetAllBy(UserInfo.UserInfoFields.Department_Code, deptCode);
        }
        else
        {
            this.ddlUsers.DataSource = new UserInfoFactory().GetAll();
        }

        this.ddlUsers.DataTextField = UserInfo.UserInfoFields.LastName.ToString();// +", " + UserInfo.UserInfoFields.LastName.ToString();
        this.ddlUsers.DataTextFormatString = "Ms {0}";
        this.ddlUsers.DataValueField = UserInfo.UserInfoFields.User_Code.ToString();
        this.ddlUsers.DataBind();
    }

    private void BindTheUD(int userCode)
    {
        object dataObject = null;
        new SqlFactory().ExecuteQuery("MAN_Select_UploadedDocumentByOwner " + userCode, out dataObject, SqlFactory.DataObjectType.DataSet);

        if (dataObject != null)
        {

            gvUploadedDocs.DataSource = dataObject;
            gvUploadedDocs.DataBind();
        }
    }

    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlDept.SelectedIndex >= 0)
        {
            BindTheUsers(int.Parse(ddlDept.SelectedValue));
        }
        else
        {
            BindTheUsers(0);
        }
    }

    protected void btnShare_Click(object sender, EventArgs e)
    {
        if (hdfItemType.Value == "Message")
        {
            UserMessage uMsg = new UserMessage();
            uMsg.Message_Code = int.Parse(hdfItemCode.Value);
            uMsg.User_Code = int.Parse(ddlUsers.SelectedValue);
            uMsg.Is_Starred = false;
            uMsg.Is_Read = false;
            uMsg.Is_Deleted = false;

            if (new UserMessageFactory().Insert(uMsg))
            {
                lblStatus.Text = "Shared with " + ddlUsers.SelectedItem.Text;
            }
            else
            {
                lblStatus.Text = "Not Shared with " + ddlUsers.SelectedItem.Text;
            }
        }
        else if (hdfItemType.Value == "Document")
        {
            UserDocument uDoc = new UserDocument();
            uDoc.User_Code = int.Parse(ddlUsers.SelectedValue);
            uDoc.Document_Code = int.Parse(hdfItemCode.Value);
            uDoc.Shared_By = _author.user_Code;
            uDoc.Shared_Date = DateTime.Now;
            uDoc.Share_Comment = "Check this out!";

            if (new UserDocumentFactory().Insert(uDoc))
            {
                lblStatus.Text = "Shared with " + ddlUsers.SelectedItem.Text;
            }
            else
            {
                lblStatus.Text = "Not Shared with " + ddlUsers.SelectedItem.Text;
            }
        }
        btnShare.Style["display"] = "inline";
        lblStatus.Visible = true;
    }
}
